package com.bizmerce.rnd.oms.criterion.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bizmerce.rnd.oms.criterion.service.CriterionService;
import com.bizmerce.rnd.oms.criterion.vo.ConfirmDocIssueVO;
import com.bizmerce.rnd.oms.criterion.vo.SendbackVO;
import com.bizmerce.rnd.oms.criterion.vo.SupplOrigInfoVO;
import com.bizmerce.rnd.oms.criterion.vo.SupplPartNoVO;
import com.bizmerce.rnd.oms.util.PagingUtil;
import com.bizmerce.rnd.oms.util.ParamMap;

import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;


/**
 * 원재료원산지관리 controller
 * 
 * @author 김장수
 */
@Controller
public class CriterionController {
	
	Log log = LogFactory.getLog(CriterionController.class);

	@Resource(name="criterionService")
	CriterionService criterionService;
	
	/**
	 * 미결관리-조회 (원산지확인서를 제출하지 않은 원재료 품목 조회)
	 */
	@RequestMapping("/criterion/oms09_select.do")
	public String selectUnsettledParts(ParamMap param, Model model) {
	    
	    param.put("stat", "A");
	    	    
	    PaginationInfo paginationInfo = PagingUtil.getPaginationInfo(param,5);
	    
	    List <SupplPartNoVO> result = criterionService.selectUnsettledParts(param);
	    String agreeNames = criterionService.selectApplyAgreeNames();
        
	    if (result.size() > 0) {
	    	paginationInfo.setTotalRecordCount(result.get(0).getTotalCount());
	    } else {
	    	paginationInfo.setTotalRecordCount(0);
	    }
        model.addAttribute("paginationInfo", paginationInfo);	    		
        model.addAttribute("searchPartNo", param.get("searchPartNo"));		
		model.addAttribute("result", result);		
		model.addAttribute("agreeNames", agreeNames);		

		return "codemng/oms09";
	}
	
	/**
	 * 미결관리-저장
	 */
	@RequestMapping("/criterion/oms09_save.do")
	public String saveUnsettledParts(ParamMap param, Model model) {
		criterionService.saveUnsettledParts(param);
		return "forward:/criterion/oms09_select.do";
	}
	
	/**
	 * 미결관리-반송
	 */
	@RequestMapping("/criterion/oms09_sendback.do")
	public String sendbackUnsettledParts(ParamMap param, Model model) {
	    criterionService.sendbackUnsettledParts(param);
	    return "forward:/criterion/oms09_select.do";
	}
    
    /**
     * 확인서작성-조회 (확인서작성이 가능한 부품 조회)
     */
    @RequestMapping("/criterion/oms10_select.do")
    public String selectConfirmParts(ParamMap param, Model model) {
        
        PaginationInfo paginationInfo = PagingUtil.getPaginationInfo(param);
        
        // TODO 조회조건 Selectbox 임시
        List <Map> statList = new ArrayList <Map>();
        Map hm1 = new HashMap <String, String>();
        hm1.put("statName", "미결");
        hm1.put("stat", "A");
        Map hm2 = new HashMap <String, String>();
        hm2.put("statName", "반송요청");
        hm2.put("stat", "B");
        
        Map hm3 = new HashMap <String, String>();
        hm3.put("statName", "승인");
        hm3.put("stat", "S");
        
        
        statList.add(hm1);
        statList.add(hm2);
        statList.add(hm3);
        
        String searchStat = param.getString("searchStat","A");
        param.put("searchStat", searchStat);
                
        List <SupplPartNoVO> list = criterionService.selectConfirmParts(param);
        
        if(list.isEmpty()) {
        	paginationInfo.setTotalRecordCount(0);	
        } else {
        	paginationInfo.setTotalRecordCount(list.get(0).getTotalCount());
        }
        
        
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("statList",     statList);     // SelectBox 구분
        model.addAttribute("searchStat",   searchStat);   // 검색조건
        model.addAttribute("searchPartNo", param.getString("searchPartNo")); // 검색조건
        model.addAttribute("result", list);        
        
        return "codemng/oms10";
    }
	
    /**
     * 확인서작성-확인서작성 & All
     */
    @RequestMapping("/criterion/oms10_write.do")
    public String selectConfirmPartsFTA(ParamMap param, Model model) {
        // TODO 원산지결정기준 select box 임시
        List <HashMap<String,String>> ruleList = new ArrayList <HashMap<String,String>>();
        HashMap <String, String> map = new HashMap <String, String>();
        map.put("rule", "CTH");
        ruleList.add(map);
        map = new HashMap <String, String>();
        map.put("rule", "BD");
        ruleList.add(map);
        map = new HashMap <String, String>();
        map.put("rule", "BU");
        ruleList.add(map);
        
        String searchStat = param.getString("searchStat","A");
        param.put("searchStat", searchStat);
        
        model.addAttribute("ruleList", ruleList); 
        model.addAttribute("result", criterionService.selectConfirmPartsFTA(param)); 
        model.addAttribute("agree", criterionService.selectApplyAgree()); 
        
        return "codemng/oms11"; // 상세화면으로.
    }
    
    /**
     * 확인서작성(상세)-제출
     */
    @RequestMapping("/criterion/oms11_save.do")
    public String insertConfirmPartsFTA(ParamMap param, Model model) {
        criterionService.insertConfirmPartsFTA(param);
        
        return "forward:/criterion/oms12_select.do";
    }
    
    /**
     * 원산지확인서발급관리-PART_NO로 조회
     */
    @RequestMapping("/criterion/oms12_select.do")
    public String selectConfirmDocIssue1(ParamMap param, Model model) {
        
        PaginationInfo paginationInfo = PagingUtil.getPaginationInfo(param);
        
        List <SupplOrigInfoVO> list = criterionService.selectConfirmDocIssue1(param);
        
        paginationInfo.setTotalRecordCount(list.get(0).getTotalCount());
        
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchPartNo", param.getString("searchPartNo")); // 검색조건      
        model.addAttribute("result", list);
        
        return "codemng/oms12";
    }
    
    /**
     * 원산지확인서발급관리-확인서번호로 조회
     */
    @RequestMapping("/criterion/oms13_select.do")
    public String selectConfirmDocIssue2(ParamMap param, Model model) {

        PaginationInfo paginationInfo = PagingUtil.getPaginationInfo(param);
        
        List <ConfirmDocIssueVO> list = criterionService.selectConfirmDocIssue2(param);
        
        paginationInfo.setTotalRecordCount(list.get(0).getTotalCount());
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchCoIssono", param.getString("searchCoIssono")); // 검색조건      
        model.addAttribute("result", list);
        
        return "codemng/oms13";
    }
    
    /**
     * 반송관리-조회
     * 
     * 상태코드 (A-미결, B-반송요청, C-반송승인, D-확인서발급)
     */
    @RequestMapping("/criterion/oms14_select.do")
    public String selectSendback(ParamMap param, Model model) {
        
        PaginationInfo paginationInfo = PagingUtil.getPaginationInfo(param);
        
        String searchStat = param.getString("searchStat",""); // 기본 전체조회
        param.put("searchStat", searchStat);
        
        List <SendbackVO> list = criterionService.selectSendback(param); // 반송부품 조회
        String agreeNames = criterionService.selectApplyAgreeNames();    // FTA협정 조회
        
        paginationInfo.setTotalRecordCount(list.get(0).getTotalCount());
        
        model.addAttribute("paginationInfo", paginationInfo);
        model.addAttribute("searchStat",   searchStat);
        model.addAttribute("searchCmpyCd", param.getString("searchCmpyCd"));        
        model.addAttribute("result", list);
        model.addAttribute("agreeNames", agreeNames);
        
        // TODO 조회조건 Selectbox 임시
        List <Map> statList = new ArrayList <Map>();
        Map hm0 = new HashMap <String, String>();
        hm0.put("statName", "전체");
        hm0.put("stat", "");
        Map hm1 = new HashMap <String, String>();
        hm1.put("statName", "반송요청");
        hm1.put("stat", "B");
        Map hm2 = new HashMap <String, String>();
        hm2.put("statName", "반송승인");
        hm2.put("stat", "C");
        statList.add(hm0);
        statList.add(hm1);
        statList.add(hm2);        
        model.addAttribute("statList", statList);
        
        return "codemng/oms14";
    }
    
    /**
     * 반송관리-반송승인
     */
    @RequestMapping("/criterion/oms14_approve.do")
    public String approveSendBack(ParamMap param, Model model) {
        criterionService.approveSendBack(param);        
        return "forward:/criterion/oms14_select.do";
    }
    
    /**
     * 반송관리-재요청
     */
    @RequestMapping("/criterion/oms14_request.do")
    public String requestSendBack(ParamMap param, Model model) {
        criterionService.requestSendBack(param);
        return "forward:/criterion/oms14_select.do";
    }	
	
}
